/**
 * @author hexj
 * @desc 订单列表组件
 * @time 2018-08-14
 */
const app = getApp(),
  {
    $resHost
  } = app.globalData,
  wxOrderList = 'wxOrderList';
Component({
  properties: {
    list: {
      //订单列表
      type: Array,
      value: []
    },
    getCompaniesData: {
      //订单列表
      type: Array,
      value: []
    },
    type: {
      //1=用户 2=员工
      type: String,
      value: ''
    },
    stype: {
      //分段
      type: String,
      value: ''
    },
    offline: {
      type: String,
      value: ''
    },
    isVideo: {
      type: String,
      value: ''
    },
    isNew: {
      type: Boolean,
      value: false
    },
    orderType: {
      type: String,
      value: ''
    }
  },
  data: {
    id: '',
    idx: '',
    isShow: false,
    company: '',
    number: '',
    serviceInfo: {},
    order_no: `${$resHost}/img/miniprogram/order/order_no.png`,
    order_over: `${$resHost}/img/miniprogram/order/order_over.png`,
    toggle: false,
    curIcon: `${$resHost}/img/cur-icon.png`,
    listMoreIndex: 0,
    listMoretext: ''
  },
  methods: {
    /**
     * 联系客户
     */
    contactTap(event) {
      let {
        id
      } = event.currentTarget.dataset;
      app.$getData('/user/goods/get-info', {
        goodsId: id
      }).then(data => {
        if (data.errCode == 0) {
          let result = data.data;
          let serviceInfo = {},
            {
              shareEmployeeId
            } = app.getTrack();
          serviceInfo = {
            orgId: result.orgId,
            serviceId: shareEmployeeId || ''
          };
          this.setData({
            toggle: true,
            serviceInfo: serviceInfo
          });
        }
      });
    },
    /**
     * 跳转到商品详情
     */
    detailTap(event) {
      let {
        id
      } = event.currentTarget.dataset;
      if (this.data.offline == '') {
        xhs.navigateTo({
          url: `${this.data.isNew ? '/pages/Employee/OrderVerify/Check/index' : '/pages/User/Goods/Order/detail/details'}?id=${id}&isEmployee=true`
        });
      }
    },
    /**
     * 查看发货信息
     */
    fectchInfoTap(e) {
      let {
        company,
        number
      } = e.target.dataset;
      xhs.showModal({
        title: '发货信息',
        content: `快递：${company}\r\n` + `单号：${number}`,
        success(res) {
          if (res.confirm) {
            console.log('用户点击确定');
          } else if (res.cancel) {
            console.log('用户点击取消');
          }
        }
      });
    },
    /**
     * 增加发货信息
     */
    addInfoTap(e) {
      let {
        id,
        idx,
        ids,
        company,
        number
      } = e.target.dataset;
      if (this.data.isNew) {
        this.setData({
          id: id,
          idx: idx,
          ids,
          isShow: true,
          company: company,
          number: number
        });
      } else {
        xhs.navigateTo({
          url: `/pages/Employee/OrderVerify/Delivery/delivery?id=${id}&query=${idx}`
        });
      }
    },
    // 到店自取
    personalTap(e) {
      let {
        id
      } = e.target.dataset;
      this.setData({
        id: id
      });
      xhs.showModal({
        title: `确定使用到店自提完成该订单吗？`,
        content: '',
        showCancel: true,
        success: res => {
          if (res.confirm) {
            this.toSolve();
          } else if (res.cancel) {
            console.log('用户点击取消');
          }
        }
      });
    },
    /**
     * 绑定快递公司输入
     */
    bindCompanyInput(e) {
      let company = e.detail.value;
      if (company) {
        this.setData({
          company: company
        });
      }
    },
    /**
     * 绑定快递单号输入
     */
    bindNumberInput(e) {
      let number = e.detail.value;
      if (number) {
        this.setData({
          number: number
        });
      }
    },
    /**
     * @api 添加发货信息
     * @param int orderId 订单id
     * @param string expressCompany 快递公司
     * @param string expressNumber 快递单号
     */
    submit() {
      let {
        list,
        id,
        idx,
        ids,
        company,
        number,
        isNew,
        isVideo,
        listMoretext
      } = this.data;
      if (isVideo == 1) {
        if (listMoretext == '') {
          return xhs.showToast({
            icon: 'none',
            title: '请选择快递公司'
          });
        }
      } else {
        if (company == '') {
          return xhs.showToast({
            icon: 'none',
            title: '请输入快递公司名称'
          });
        }
      }
      if (number == '') {
        return xhs.showToast({
          icon: 'none',
          title: '请输入快递单号'
        });
      }
      let param = {
        orderId: id,
        subOrderId: 0,
        expressCompany: company,
        expressNumber: number,
        deliveryType: 1
      };
      if (isVideo == 1) {
        param = {
          orderId: id,
          expressCompany: listMoretext,
          expressNumber: number,
          deliveryType: 1
        };
      }
      if (isNew) {
        param.subOrderId = ids;
      }
      app.$getData(isNew ? '/employee/ec-order/writeoff' : '/employee/order/writeoff', app.$parseParam(param, wxOrderList)).then(data => {
        if (data.errCode == 0) {
          xhs.showToast({
            icon: 'none',
            title: '核销成功'
          });
          if (isVideo == 1) {
            this.setData({
              listMoretext: ''
            });
          }
          list[idx].expressNumber = number;
          list[idx].expressCompany = company;
          this.setData({
            list: list
          });
          this.toUpdate();
          this.toggle();
        } else if (data.errCode == 21054) {
          //点核销确定时不用传isEmptyProfit，接口返回errCode：21054时，弹窗提示：分账比例超限，本次订单不分账，点击确定再调用核销接口，这次就要传isEmptyProfit = 1
          xhs.showModal({
            title: '提示',
            content: `分账比例超限，本次订单不分账`,
            success(res) {
              if (res.confirm) {
                param.isEmptyProfit = 1;
                app.$getData(isNew ? '/employee/ec-order/writeoff' : '/employee/order/writeoff', app.$parseParam(param, wxOrderList)).then(data => {
                  if (data.errCode == 0) {
                    xhs.showToast({
                      icon: 'none',
                      title: '添加成功'
                    });
                    list[idx].expressNumber = number;
                    list[idx].expressCompany = company;
                    this.setData({
                      list: list
                    });
                    this.toUpdate();
                    this.toggle();
                  }
                });
              } else if (res.cancel) {
                console.log('用户点击取消');
              }
            }
          });
        }
      });
    },
    /**
    * @api 添加到店自提
    * @param int $orderId 订单ID
    */
    toSolve() {
      let {
        id,
        ids,
        isNew
      } = this.data;
      let param = {
        orderId: id,
        deliveryType: 2,
        subOrderId: 0
      };
      if (isNew) {
        param.subOrderId = ids;
      }
      app.$getData(isNew ? '/employee/ec-order/writeoff' : '/employee/order/writeoff', app.$parseParam(param, wxOrderList)).then(data => {
        if (data.errCode == 0) {
          xhs.showToast({
            icon: 'none',
            title: '添加成功'
          });
          this.toUpdate();
          this.toggle();
        } else if (data.errCode == 21054) {
          //点核销确定时不用传isEmptyProfit，接口返回errCode：21054时，弹窗提示：分账比例超限，本次订单不分账，点击确定再调用核销接口，这次就要传isEmptyProfit = 1
          xhs.showModal({
            title: '提示',
            content: `分账比例超限，本次订单不分账`,
            success(res) {
              if (res.confirm) {
                param.isEmptyProfit = 1;
                app.$getData(isNew ? '/employee/ec-order/writeoff' : '/employee/order/writeoff', app.$parseParam(param, wxOrderList)).then(data => {
                  if (data.errCode == 0) {
                    xhs.showToast({
                      icon: 'none',
                      title: '添加成功'
                    });
                    this.toUpdate();
                    this.toggle();
                  }
                });
              } else if (res.cancel) {
                console.log('用户点击取消');
              }
            }
          });
        }
      });
    },
    /**
    * @api 添加到店自提
    * @param int $orderId 订单ID
    */
    toUpdate() {
      console.log('嚄嚄嚄嚄嚄嚄嚄=====');
      let {
        stype,
        isNew,
        orderType
      } = this.data;
      let param = {
        status: stype,
        orderType: orderType
      };
      let url = isNew ? '/employee/ec-order/goods-order-list' : '/employee/order/goods-order-list';
      console.log('this.data.isVideo===', this.data.isVideo);
      if (this.data.isVideo == 1) {
        url = '/employee/ec-order/goods-order-list-for-video-account';
      }
      app.$getData(url, app.$parseParam(param, wxOrderList)).then(data => {
        if (data.errCode == 0) {
          this.setData({
            list: data.data.map(item => {
              item.goods = isNew ? item.goodsList : Array.isArray(item.goods) ? item.goods : [item.goods];
              return item;
            })
          });
        }
      });
    },
    toggle() {
      this.setData({
        isShow: false
      });
    },
    callPhone: function (event) {
      let {
        phone
      } = event.currentTarget.dataset;
      xhs.makePhoneCall({
        phoneNumber: phone
      });
    },
    listMoreChanges(e) {
      let {
        getCompaniesData,
        listMoreIndex
      } = this.data;
      this.setData({
        listMoreIndex: e.detail.value,
        listMoretext: getCompaniesData[e.detail.value].deliveryName
      });
    },
    parseData(data, num) {
      return data.toFixed(num);
    }
  }
});